package hadoop.mr09_JOIN;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;

public class JoinReducer extends Reducer<Text,Text, NullWritable,Text> {
    // 创建集合保存详细成绩和总成绩的数据，便于join关联
    ArrayList<String> a1 = new ArrayList<String>(); // 详细成绩
    ArrayList<String> a2 = new ArrayList<String>(); // 总成绩
    Text out_value = new Text();
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, NullWritable, Text>.Context context) throws IOException, InterruptedException {
        for (Text value:values) {
            if(value.toString().startsWith("ALL#")){
                String s = value.toString().split("#")[1];
                a1.add(s);
            }
            if(value.toString().startsWith("SUM#")){
                String s = value.toString().split("#")[1];
                a2.add(s);
            }
        }
        // 获取集合长度
        int a1_len = a1.size();
        int a2_len = a2.size();
        for (int i = 0; i < a1_len; i++) {
            for (int j = 0; j < a2_len; j++) {
                // out_Value = 2305151131,李美玲,1,68,80,79,60,87,94,94,64,女,699
                out_value.set(a1.get(j)+","+a2.get(i));
                System.out.println(key+"----------"+out_value.toString());
                context.write(NullWritable.get(),out_value);
            }
        }
        // 秦空集合
        a1.clear();
        a2.clear();
    }
}
